package dao

import (
	"Im/dto"
	"Im/global"
	"Im/model"
	"errors"
	"time"
)

func ImUserLogin(Account string, Password string) (error, model.ImUser) {
	var user model.ImUser
	result := global.SqlDb.Where(map[string]any{
		"account":  Account,
		"password": Password,
	}).Find(&user)
	return result.Error, user
}
func ImUserSignUp(userDto dto.ImUserSignDto) (model.ImUser, error) {
	var user model.ImUser = model.ImUser{
		Account:  userDto.Account,
		Password: userDto.Password,
		Nickname: userDto.Nickname,
		Email:    userDto.Email,
		CreateAt: time.Now().Format("2006/01/02 15:04:05"),
		UpdateAt: time.Now().Format("2006/01/02 15:04:05"),
	}
	result := global.SqlDb.Where(map[string]any{
		"account": userDto.Account,
	}).Or("nickname=?", userDto.Nickname).Find(&user)
	if result.RowsAffected > 0 {
		return user, errors.New("用户名或账号已被注册")
	}
	//到这意味着未找到相同的数据，可以开始进行注册
	result = global.SqlDb.Create(&user)
	return user, result.Error
}
